package edu.xkd.slice;

import edu.xkd.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.global.icu.util.TimeUnit;

import java.sql.Time;

public class MainAbilitySlice extends AbilitySlice {
    int num = 0;
    boolean flag = true;
    long startTime = 0;
    Text text_show = null;
    Button button_01 = null;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //初始化
        startTime = System.currentTimeMillis();
        text_show = (Text)this.findComponentById(ResourceTable.Id_text_show);
        button_01 = (Button) this.findComponentById(ResourceTable.Id_button_01);

        //为Button添加点击事件
        button_01.setClickedListener(this::onClickedListenerButton01);
    }

    private void onClickedListenerButton01(Component component) {
        long currentTime = System.currentTimeMillis();
        if(flag){
            num++;
            //第一次点击
            this.flag = false;
            button_01.setText("请疯狂点击");
            text_show.setText(num+"");
        }else{
            //不是第一次点击,分为两种情况，一种未超时，一种超时
            if(currentTime - startTime <= 60000){
                num++;
                text_show.setText(num+"");
            }else{
                button_01.setText("结束");
                button_01.setClickable(false);
                text_show.setText("您共点击了:"+num+"次!!");
            }
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
